package com.nicolasprof.profgallery.preloader {
	import gs.TweenLite;

	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.assets.PreloaderBG;
	import com.nicolasprof.profgallery.assets.PreloaderBar;

	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;

	/**
	 * @author Nicolas Prof
	 */
	public class Preloader extends Sprite {

		private var _bar : PreloaderBar;
		private var _width : Number;
		private var _height : Number;
		protected const SHADOW : DropShadowFilter = new DropShadowFilter(0,45,0,1,4,4,0.4);
		
		
		public function Preloader() {
		}
		
		public function initiate() : void {
			_bar = new PreloaderBar();
			var bg: PreloaderBG = new PreloaderBG();
			var config : XML = Engine.CONFIG_XML;
			var margin : int = 2; //by design
			
			_width = Number(config.preloaderSimple.@width);
			_height = Number(config.preloaderSimple.@height);
			
			//Dispplay List
			addChild(bg);
			addChild(_bar);

			bg.filters = [SHADOW];
			bg.width = _width;
			bg.height = _height;
			
			bg.x =  -_width>>1;
			bg.y =  -_height>>1;
			
			_bar.width = _width - 2 * margin;
			_bar.height = _height - 2 * margin;
			
			_bar.x = (-_width >> 1) + margin;
			_bar.y = (-_height >> 1) + margin;
		}

		public function toggle(state : Boolean, speed: Number = 0.5) : void {
			if(state) {
				TweenLite.to(this, speed, {autoAlpha:1});
			}else{
				TweenLite.to(this, speed, {autoAlpha:0});
			}
		}
		
		/**
		 * GETTERS AND SETTERS
		 */
		/**
		 * Set Progress Ratio
		 */
		public function set progress(ratio : Number) : void {
			var margin : int = 2; //by design
			_bar.width = ratio * (_width - 2 * margin);
		}
	}
}
